/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AlertSystem;
import javafx.scene.Node;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.scene.control.TextBox;
import javafx.scene.control.PasswordBox;
import javafx.scene.control.Button;
import java.sql.ResultSet;
import javafx.scene.paint.Color;
import javafx.scene.input.MouseEvent;

/**
 * @author quang
 */

public class AuthenticationBox extends Window {
    public var userName: String = "charmgil";
    public var isLogin: Boolean = true;

    public override function create():Node {
        def textBox = TextBox {
            columns: 12
            font: font2
            selectOnFocus: true
            onMouseClicked: function(e: MouseEvent): Void {
                textMessage = "";
            }
        }

        def passwordBox = PasswordBox {
            columns: 12
            font: font2
            selectOnFocus: true
            onMouseClicked: function(e: MouseEvent): Void {
                textMessage = "";
            }
        }
        
        var textMessage: String = "";

        def resSelector = ResolutionSelector{};

        return VBox {
            spacing: 15
            content: [
                Text {
                    content: bind textMessage
                    fill: Color.RED
                    font: font2
                },
                HBox {
                    spacing: 10
                    content: [
                        Text {
                            content: "User Name:"
                            font: font2
                        },
                        textBox
                    ]
                },
                HBox {
                    spacing: 10
                    content: [
                        Text {
                            content: "Password: "
                            font: font2
                        },
                        passwordBox
                    ]
                }
                Button {
                    text: "Sign In"
                    font: font2
                    action: function() {
                        Window.screenWidth = resSelector.getScreenWidth();
                        Window.screenHeight = resSelector.getScreenHeight();

                        var loginRS: ResultSet = DBConnection.query (connection,
                        "SELECT * FROM {inDBName}.account WHERE username = '{textBox.text}' AND password = '{passwordBox.text}'");
                        if (loginRS.first() == false) {
                            textMessage = "Invalid username or password. Please try again.";
                        }
                        else {
                            this.userName = textBox.text;
                            this.isLogin = true;
                            passwordBox.clear();
                        }
                    }
                },
                resSelector
            ]
        };
    }

}
